/**
 * @file rk_aiq.c
 * @brief Rockchip AIQ service handling implementation.
 */

#include <stdio.h>
#include <string.h>
#include "rk_aiq.h"
#include "util.h"

rk_aiq_sys_ctx_t *aiq_init(const char *sensor_entity_name, const char *iq_files_path, int width, int height)
{
    printf("AIQ: Initializing for sensor %s...\n", sensor_entity_name);

    rk_aiq_sys_ctx_t *ctx = rk_aiq_uapi2_sysctl_init(sensor_entity_name, iq_files_path, NULL, NULL);
    if (!ctx)
    {
        fprintf(stderr, "ERROR: rk_aiq_uapi2_sysctl_init failed!\n");
        return NULL;
    }

    int ret = rk_aiq_uapi2_sysctl_prepare(ctx, width, height, RK_AIQ_WORKING_MODE_NORMAL);
    if (ret < 0)
    {
        fprintf(stderr, "ERROR: rk_aiq_uapi2_sysctl_prepare failed! (%d)\n", ret);
        rk_aiq_uapi2_sysctl_deinit(ctx);
        return NULL;
    }
    printf("AIQ: Prepare success.\n");

    ret = rk_aiq_uapi2_sysctl_start(ctx);
    if (ret < 0)
    {
        fprintf(stderr, "ERROR: rk_aiq_uapi2_sysctl_start failed! (%d)\n", ret);
        rk_aiq_uapi2_sysctl_deinit(ctx);
        return NULL;
    }
    printf("AIQ: Start success. AIQ service is running.\n");

    return ctx;
}

void aiq_deinit(rk_aiq_sys_ctx_t *ctx)
{
    if (ctx)
    {
        printf("AIQ: Stopping and de-initializing...\n");
        rk_aiq_uapi2_sysctl_stop(ctx, false);
        rk_aiq_uapi2_sysctl_deinit(ctx);
        printf("AIQ: De-init success.\n");
    }
}

